﻿--Select * from tmp;
CREATE OR REPLACE FUNCTION fn_check_constraint_project_budget_advance(budget_id int,adv_id int)  RETURNS boolean AS
$BODY$
declare
	chk_c boolean;
begin
	if exists(Select kbd.id from kdvn_budget_data kbd where kbd.project_id=(Select project_id from kderp_advance_payment where kderp_advance_payment.id=$2) and kbd.budget_id=$1) then
		chk_c = true;
	else
		chk_c = false;
	end if;
	return chk_c;
end;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

CREATE OR REPLACE FUNCTION fn_check_constraint_project_budget_ot(budget_id int,ot_id int)  RETURNS boolean AS
$BODY$
declare
	chk_c boolean;
begin
	if exists(Select kbd.id from kdvn_budget_data kbd where project_id=(Select project_id from kderp_other_expense where id=$2) and kbd.budget_id=$1) then
		chk_c = true;
	else
		chk_c = false;
	end if;
	return chk_c;
end;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;